package net.wofly.right.util;

import net.wofly.common.util.ResultCode;
import net.wofly.common.util.ReturnBean;
import net.wofly.right.domain.RegisterVerifyInfo;
import net.wofly.right.service.IRegisterVerifyInfoService;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/12/17.
 */
public class VerifyCodeCheck {
    public static ReturnBean<String> verifyCodeCheck(IRegisterVerifyInfoService iRegisterVerifyInfoService, String mobile,
                                                     String verifyCode){
        //校验验证码
        List<RegisterVerifyInfo> registerVerifyInfos = iRegisterVerifyInfoService
                .findByRegisterAccountOrderByRegisterTimeDesc(mobile);
        if (CollectionUtils.isEmpty(registerVerifyInfos)) {
            return new ReturnBean<>(-11, "手机号不正确或者验证码失效！");
        }

        RegisterVerifyInfo registerVerifyInfo = registerVerifyInfos.get(0);
        if (registerVerifyInfo.getInvalidTime() < System.currentTimeMillis()) {
            return new ReturnBean<>(-12, "验证码失效，请重新申请发送验证码！");
        }

        if (!verifyCode.equals(registerVerifyInfo.getVerifyCode())) {
            return new ReturnBean<>(-13, "验证码错误！");
        }

        return new ReturnBean<>(ResultCode.SUCCESS, "验证码验证通过！");
    }
}
